package zj.com.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import zj.com.adapter.JcrRightAdapter;
import zj.com.model.User;
import zj.com.model.UserInfo;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.util.SharedPreferencesUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/17.
 */
public class ZgCheckActivity extends  BaseActivity implements View.OnClickListener{


    public ListView listView;
    public TextView title_name;
    public ImageView left_iv;
    public JcrRightAdapter adapter;
    public List<User> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_fen);
        initView();
        initData();
    }

    public void initView(){
        list = new ArrayList<>();

        listView = (ListView) findViewById(R.id.listView);
        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("选择");


        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setOnClickListener(this);
        left_iv.setVisibility(View.VISIBLE);
    }

    public void initData(){



        adapter = new JcrRightAdapter(list,this);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent();
                intent.putExtra("user", list.get(position));
                setResult(RESULT_OK, intent);
                finish();
            }
        });


    }


    public ProgressDialog dialog;
    @Override
    protected void onResume() {
        super.onResume();
        obtainData();
    }

    public void obtainData(){

        Object obj =  SharedPreferencesUtil.readObject(this, "user");
        UserInfo uinfo = null;
        if(obj == null){
            Toast.makeText(ZgCheckActivity.this, "请在个人中心 获取个人信息", Toast.LENGTH_SHORT).show();
            return;
        }
        uinfo =(UserInfo)obj;

        dialog = DialogUtil.createProgressDialog(this,"提示","获取数据中...");
        NetManager.getInstance(this).GetUserListByKeyWord("","repair",uinfo.getOrgCode() + "", new OnResponseListener<List<User>>() {
            @Override
            public void onComplete(boolean isOK, List<User> result, int errorCode, String errorMsg) {

                if(isOK){

                    if(result != null && result.size() > 0){

                        list = result;
                        adapter.setDataChange(result);
                    }else{
                        list.clear();
                        adapter.setDataChange(list);
                    }
                }else{
                    Toast.makeText(ZgCheckActivity.this, "获取失败", Toast.LENGTH_SHORT).show();

                }
                dialog.dismiss();
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                Toast.makeText(ZgCheckActivity.this, "获取失败", Toast.LENGTH_SHORT).show();

            }
        });
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.left_iv:
                finish();
                break;
        }
    }
}
